<?php

Yii::import('common.models._base.BaseReviews');

class Reviews extends BaseReviews {

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public static function getAllReview($type, $classId) {
        $criteria = new CDbCriteria;
        $criteria->condition = $type . '=:' . $type;
        $criteria->params = array($type => $classId);
        $criteria->order = 'RAND()';
        $criteria->limit = 2;
        return $reviewdetails = Reviews::model()->findAll($criteria);
    }

    public static function saveReviewAfterloginuser() {
        $reviewdata = Yii::app()->session['class_course_review'];
        $model = new Reviews;
        $userid = getLoginUserId();
        if (isset($reviewdata['classId'])) {
            $classCourseid = $reviewdata['classId'];
            $type = SkillConfig::class_name;
            $model->setAttribute('classes_id', $reviewdata['classId']);
            $classCourseUser = Classes::model()->findByPk($reviewdata['classId']);
            $classCourseUser = ($classCourseUser->users_id != '') ? $classCourseUser->users_id : '';
            $reviewsUser = $model->findByAttributes(array('classes_id' => $reviewdata['classId'], 'users_id' => $userid));
        } if (isset($reviewdata['courseId'])) {
            $type = SkillConfig::course_name;
            $classCourseid = $reviewdata['courseId'];
            $model->setAttribute('courses_id', $reviewdata['courseId']);
            $classCourseUser = Courses::model()->findByPk($reviewdata['courseId']);
            $classCourseUser = ($classCourseUser->users_id != '') ? $classCourseUser->users_id : '';
            $reviewsUser = $model->findByAttributes(array('courses_id' => $reviewdata['courseId'], 'users_id' => $userid));
        }

        if ($reviewsUser <> "" && $classCourseUser != $userid && $reviewsUser->users_id != $userid) {
            $model->setAttribute('reviews_text', $reviewdata['text']);
            $model->setAttribute('users_id', $userid);
            $model->setAttribute('status', SkillConfig::STATUS_ACTIVE);
            $model->setAttribute('create_time', SkillConfig::CreateDateTime());
            if ($model->save(FALSE)) {
                echo Message::REVIEWS_SUCCESS_MSG;
            }
        } elseif ($reviewsUser == "" && $classCourseUser != $userid) {
            $model->setAttribute('reviews_text', $reviewdata['text']);
            $model->setAttribute('users_id', $userid);
            $model->setAttribute('status', SkillConfig::STATUS_ACTIVE);
            $model->setAttribute('create_time', SkillConfig::CreateDateTime());
            if ($model->save(FALSE)) {
                $errorCode = Message::success;
            }
        }

        if ($reviewsUser <> "" && ($reviewsUser->users_id == $userid)) {
            $errorCode = Message::repeat_user;
        }

        if ($classCourseUser == $userid) {
            $errorCode = Message::owan_user;
        }
        unset(Yii::app()->session['class_course_review']);
        return Yii::app()->createAbsoluteUrl('reviews/userreviews', array('type' => $type, 'skillusers' => $userid, 'workshop' => $classCourseid, 'skillcode' => $errorCode));
    }

}
